#include "userinfoform.h"

#include "ui_userinfoform.h"

#include <QDebug>
#include <CommonUtils.h>
#include "edituserinfoform.h"
#include <Application.h>

UserInfoForm::UserInfoForm(QWidget* parent)
    : BasicWindows(parent),
      ui(new Ui::UserInfoForm) {
    ui->setupUi(this);

    CommonUtils::loadStyleSheet(this, "userinfoform");
    connect(ui->btn_edit, &QPushButton::clicked, this, &UserInfoForm::onEditInfoButtonClicked);
}

UserInfoForm::~UserInfoForm() {
    delete ui;
}

void UserInfoForm::setUserInfo(const data::UserInfo& userInfo) {
    ui->label_headshot->setHeadshot(QString::fromStdString(userInfo.headshot()));
    ui->label_name->setText(userInfo.name().c_str());
    ui->label_userId->setText(userInfo.userid().c_str());
    ui->label_sign->setText(userInfo.sign().c_str());
    ui->label_email->setText(userInfo.email().c_str());
    ui->label_phone->setText(userInfo.phonenumber().c_str());
    if (userInfo.sex() == "男") {
        ui->label_sex->setPixmap(QPixmap(":/resource/img/mainwindow/userInfo/sexm.png"));
    }
    else if (userInfo.sex() == "女") {
        ui->label_sex->setPixmap(QPixmap(":/resource/img/mainwindow/userInfo/sexw.png"));
    }
    else {
        ui->label_sex->setPixmap(QPixmap(":/resource/img/mainwindow/userInfo/sexNone.png"));
    }
}

void UserInfoForm::onEditInfoButtonClicked() {
    EditUserInfoForm form;
    this->hide();
    form.exec();
    setUserInfo(Application::Myself());
    this->close();
    

}

bool UserInfoForm::eventFilter(QObject* obj, QEvent* event) {
    if (!isActiveWindow()) {
        // 如果点击了其他窗口，关闭当前子窗口
        if (isVisible()) {
            qDebug() << "ObjiectName:" << obj->objectName() << "   setVisible:" << false;
            setVisible(false);
        }
    }
    return QWidget::eventFilter(obj, event);
}
